package me.helllp.book.concurrent.ch05;

import java.nio.ByteBuffer;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import io.netty.util.concurrent.DefaultThreadFactory;

/**
 * 使用Disruptor作为缓冲区实现生产者-消费者模式
 * 
 * @author Administrator
 *
 */
public class DisruptorDemo {
	public static void main(String[] args) throws InterruptedException {
		
		PCDataDisruptorFactory factory = new PCDataDisruptorFactory();
		
		int bufferSize = 1024;
		
		Disruptor<PCDataDisruptor> disruptor = new Disruptor<PCDataDisruptor>(factory, bufferSize, new DefaultThreadFactory("Disruptor"));

		disruptor.handleEventsWithWorkerPool(
				new ConsumerDisruptor(),
				new ConsumerDisruptor(),
				new ConsumerDisruptor(),
				new ConsumerDisruptor());
		disruptor.start();
		
		RingBuffer<PCDataDisruptor> ringBuffer = disruptor.getRingBuffer();
		
		ProducerDisruptor producer = new ProducerDisruptor(ringBuffer);
		ByteBuffer bb = ByteBuffer.allocate(8);
		
		for(long l=0; true; l++){
			bb.putLong(0,l);
			producer.pushData(bb);
			Thread.sleep(100);
			System.out.println("add data "+ l);
		}
	}
}
